<template>
  <div>
    <div class="page-filter mb10 fl">
      <el-row>
        <el-col :span="24">
          <el-form ref="filter" :model="filter" label-width="0">
            <el-form-item class="mr10 mb5 fl">
              <el-input v-model="filter.name" placeholder="客户名称" size="small" clearable />
            </el-form-item>
            <el-form-item class="mr10 mb5 fl" style="width: 300px">
              <el-button type="primary" size="small" @click="getList">查 询</el-button>
              <el-button type="primary" size="small" @click="handleAdd">新 增</el-button>
              <el-button type="primary" size="small" @click="handleOut">导 出</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="客户名称" min-width="220px" prop="name" />
      <el-table-column label="开户行" width="200px" prop="bank" />
      <el-table-column label="账户" width="200px" prop="account" />
      <el-table-column label="地址" width="220px" prop="addr" />
      <el-table-column label="税号" width="160px" prop="num" />
      <el-table-column label="负责人" width="90px" prop="director" />
      <el-table-column label="电话" align="center" width="100px" prop="phone" />
      <el-table-column label="评级" align="center" width="80px" prop="rate" />
      <el-table-column label="备注" min-width="160px" prop="remark" />
      <el-table-column label="操作" align="center" width="110px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
              <el-dropdown-item @click.native="contacts(row)">联系方式</el-dropdown-item>
              <!-- <el-dropdown-item @click.native="invoices(row)">开票信息</el-dropdown-item> -->
              <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
              <!-- <el-dropdown-item @click.native="outData(row)">导 出</el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <Contacts ref="contacts" :get-data="getList" :info="dialog" />
    <!-- <Invoices ref="invoices" :get-data="getList" :info="dialog" /> -->
  </div>
</template>

<script>
import page from '@/utils/page'
import { customers, delCus, outCustomers } from '@/api/customer'
import Pagination from '@/components/Pagination'
import Dialog from './components/customer-dialog'
import Contacts from './components/contacts-dialog'
// import Invoices from './components/invoices-dialog'

export default {
  name: '',
  components: { Pagination, Contacts, Dialog },
  extends: page,
  inject: ['app'],
  provide() {
    return {
      app: this.app
    }
  },
  data() {
    return {
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      this.app.loading = true
      customers({
        ...this.filter,
        ...e
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    contacts(e) {
      this.dialog.title = '客户联系方式'
      this.app.dialogParam(this.dialog.edit, ['ctm_id'], { 'ctm_id': e.id })
      this.$refs['contacts'].visible = true
    },
    // invoices(e) {
    //   this.dialog.title = '客户开票信息'
    //   this.app.dialogParam(this.dialog.edit, ['ctm_id'], { 'ctm_id': e.id })
    //   this.$refs['invoices'].visible = true
    // },
    // outData(e) {
    //   outCustomers({
    //     id: e['id']
    //   }).then(response => {
    //     const blob = new Blob([response.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
    //     const downloadElement = document.createElement('a')
    //     const href = window.URL.createObjectURL(blob)
    //     downloadElement.href = href
    //     downloadElement.download = decodeURI(response.headers['filename'])
    //     document.body.appendChild(downloadElement)
    //     downloadElement.click()
    //     document.body.removeChild(downloadElement)
    //     window.URL.revokeObjectURL(href)
    //   }).catch(err => {
    //     this.$message.error(err.msg)
    //   })
    // },
    handleOut() {
      outCustomers({
        ...this.filter
      }).then(response => {
        this.out(response)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delCus({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleEdit(e) {
      this.dialog.title = '编辑客户'
      this.dialog.type = 'edit'
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'bank', 'account'
      , 'addr', 'num', 'rate', 'remark'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增客户'
      this.dialog.type = 'add'
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'bank', 'account'
      , 'addr', 'num', 'rate', 'remark'], e)
      this.$refs['dialog'].visible = true
    }
  }
}
</script>
